import java.net.URL;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.StringTokenizer;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.HTML.Tag;

/**
 * @author vihuynh, k7ho
 * 
 */
public class NewParserCallBack extends HTMLEditorKit.ParserCallback {
	private URL base; // storing which HTML we are in
	private Hashtable<String, Hashtable<String, Integer>> bigTbl;
	// key = keyword, value = hashtable<URL, hits count>
	private LinkedList<String> list, anchorWords;
	private Hashtable<String, LinkedList<String>> anchorList;
	
	int wordCount = 0; // # of read in lines
	boolean Atag = false;
	
	public NewParserCallBack(URL inURL,
			Hashtable<String, Hashtable<String, Integer>> inTbl) {
		// initialize variables
		this.base = inURL;
		this.bigTbl = inTbl;	
		this.list = new LinkedList<String>();
		this.anchorWords = new LinkedList<String>();
		this.anchorList = new Hashtable<String, LinkedList<String>>();
	}

	@Override
	public void handleStartTag(HTML.Tag tag, MutableAttributeSet attr, int pos) {
		String aryTag = "<" + tag;
		String linkToBase = "";
		if (tag == HTML.Tag.A) {
			String link = (String) attr.getAttribute(HTML.Attribute.HREF);
			// TODO add the link to queue or stack;
			linkToBase = base.toString();
			// System.err.print(link);
			if (link.endsWith(".htm") || link.endsWith(".html")) {
				Atag = true;
				anchorWords.clear();
				// to check if abs. link 
				if (link.startsWith("http://") || link.startsWith("https://")) {
					linkToBase = link;
				}
				else {
					linkToBase = linkToBase.substring(0, linkToBase
							.lastIndexOf('/') + 1);
					linkToBase += link;
					/***TESTING***********************************************/
					//System.err.println(linkToBase);
					/***TESTING***********************************************/
				}
				aryTag += " " + linkToBase;
				list.add(linkToBase);
			}

		}
		/***TESTING***********************************************/
		aryTag += ">";
		//System.out.println(aryTag);
		/***TESTING***********************************************/
	}

	@Override
	public void handleEndTag(HTML.Tag t, int pos) {
		//String aryTag = "</" + t + ">";
		//System.out.print(aryTag);
		// TODO handle the word between between A tag,
		// Description in guide line:
		/*
		 * keep in mind that keywords found in anchor element content will count
		 * as keywords both for the page currently being visited, and for the
		 * page linked to -- if it has already been visited or will be visited
		 * later in the search. (Pages that are not actually v isited in the
		 * search should not appear in the index at all.)
		 */
		
		if(t == HTML.Tag.A) {
			Atag = false;
			anchorList.put(list.getLast(), anchorWords);
		}
	}

	@Override
	public void handleText(char[] data, int pos) {

		String str = new String(data);

		String[] result = str.split("\\W");
		String addingKeyWord = "";
		Keyword keyFilter = new Keyword();

		for (String iter : result) {
			// temp = token.nextToken();
			addingKeyWord = keyFilter.makeKeyword(iter);
			if (!addingKeyWord.equals("")) {
				String currentURL = base.toString();
				if (bigTbl.containsKey(addingKeyWord)) {
					// word already exist in big HashTable
					Hashtable<String, Integer> smallTbl = bigTbl
							.get(addingKeyWord);

					// check if the URL exist in the small hashTable
					if (smallTbl.containsKey(currentURL)) {
						// URL exists add count hits
						smallTbl.put(currentURL, smallTbl.get(currentURL) + 1);

					}
					else {
						// URL not exist in small hashtable, add new one
						smallTbl.put(currentURL, 1);
						wordCount++;
					}
					bigTbl.put(addingKeyWord, smallTbl);
					if(Atag) {
						anchorWords.add(addingKeyWord);
						/***TESTING***********************************************/
						//System.out.println("Kwong:" + addingKeyWord);
						/***TESTING***********************************************/
					}
				}
				else {
					// word not yet exist in big HashTable, add it
					Hashtable<String, Integer> small = new Hashtable<String, Integer>();
					small.put(currentURL, 1);
					bigTbl.put(addingKeyWord, small);
					wordCount++;
					if(Atag) {
						anchorWords.add(addingKeyWord);
						/***TESTING***********************************************/
						//System.out.println("Kwong:" + addingKeyWord);
						/***TESTING***********************************************/
					}
				}
				/***TESTING***********************************************/
				// System.out.println("word: " + addingKeyWord);
				/***TESTING***********************************************/
			}
		}

	}
	
	/**
	 * @return list
	 */
	public LinkedList<String> getList() {
		return list;
	}
	
	public LinkedList<String> getList2() {
		return anchorWords;
	}
	
	public Hashtable<String, LinkedList<String>> getHash() {
		return anchorList;
	}


}
